package com.atguigu.activemq.queue_delay;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ScheduledMessage;

import javax.jms.*;


public class JmsProduce {
    public static final String ACTIVEMQ_URL="tcp://124.70.84.192:61616";
    public static final String QUEUE_NAME="queue_Delay";

    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE_NAME);
        MessageProducer messageProducer = session.createProducer(queue);
        messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        //设置延迟投递属性值
        long delay=3*1000;//延迟3秒
        long period=3*1000;//每4秒延迟投递一次
        int repeat=5;//共延迟投递5次

        for (int i=1;i<4;i++){
            TextMessage textMessage = session.createTextMessage("我发送的第" + i + "条消息");
            //设置消息的属性
            textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY,delay);
            //延迟投递的时间
            textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD,period);
            //重复投递的时间间隔
            textMessage.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT,repeat);
            //重复投递次数
            messageProducer.send(textMessage);
        }
        messageProducer.close();
        session.close();
        connection.close();
        System.out.println("连接中断");
    }
}
